package com.food.restaurantpro.database;

import com.food.restaurantpro.database.tables.DishCategoryTable;
import com.food.restaurantpro.database.tables.FoodTable;
import com.food.restaurantpro.database.tables.PlaceTable;
import com.food.restaurantpro.database.tables.PromotionTable;
import com.food.restaurantpro.database.tables.ReservationTable;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.util.Log;

public class RestaurantAppContentProvider extends ContentProvider {
	private static final String TAG = "RestaurantAppContentProvider";
	/* database helper */
	DatabaseHelper mDatabaseHelper;

	/* key for uri matches */
	private static final int PROMOTION_KEY = 1;
	private static final int DISH_CATEGORY_KEY = 2;
	private static final int FOOD_KEY = 3;
	private static final int PLACE_KEY = 4;
	private static final int RESERVATION_KEY = 5;

	/* authority */
	private static final String AUTHORITY = "com.food.restaurantpro.provider";

	/* Uri Matches */
	private static UriMatcher sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

	/* path */
	private static final String PROMOTION_PATH = "promotion_path";
	private static final String DISH_CATEGORY_PATH = "dish_category_path";
	private static final String FOOD_PATH = "food_path";
	private static final String PLACE_PATH = "place_path";
	private static final String RESERVATION_PATH = "reservation_path";

	/* content uri */
	public static final Uri PROMOTION_CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + PROMOTION_PATH);
	public static final Uri DISH_CATEGORY_CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + DISH_CATEGORY_PATH);
	public static final Uri FOOD_CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + FOOD_PATH);
	public static final Uri PLACE_CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + PLACE_PATH);
	public static final Uri RESERVATION_CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/" + RESERVATION_PATH);
	
	static {
		sUriMatcher.addURI(AUTHORITY, PROMOTION_PATH, PROMOTION_KEY);
		sUriMatcher.addURI(AUTHORITY, DISH_CATEGORY_PATH, DISH_CATEGORY_KEY);
		sUriMatcher.addURI(AUTHORITY, FOOD_PATH, FOOD_KEY);
		sUriMatcher.addURI(AUTHORITY, PLACE_PATH, PLACE_KEY);
		sUriMatcher.addURI(AUTHORITY, RESERVATION_PATH, RESERVATION_KEY);
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		// TODO Auto-generated method stub
		int rowDelete = 0;
		SQLiteDatabase db = null;
		db = mDatabaseHelper.getWritableDatabase();
		int uriKey = sUriMatcher.match(uri);
		switch (uriKey) {
		case PROMOTION_KEY:
			try {
				db.beginTransaction();
				rowDelete = db.delete(PromotionTable.TABLE_NAME, selection,
						selectionArgs);
				db.setTransactionSuccessful();
			} catch (SQLException ex) {
				Log.e(TAG,
						"There was a problem with the delete in "
								+ PromotionTable.TABLE_NAME + ", selection = "
								+ selection + ", selectionArgs = "
								+ selectionArgs.toString() + ex.toString());
			} finally {
				db.endTransaction();
			}
			break;
		case DISH_CATEGORY_KEY:
			try {
				db.beginTransaction();
				rowDelete = db.delete(DishCategoryTable.TABLE_NAME, selection,
						selectionArgs);
				db.setTransactionSuccessful();
			} catch (SQLException ex) {
				Log.e(TAG,
						"There was a problem with the delete in "
								+ DishCategoryTable.TABLE_NAME + ", selection = "
								+ selection + ", selectionArgs = "
								+ selectionArgs.toString() + ex.toString());
			} finally {
				db.endTransaction();
			}
			break;
		case FOOD_KEY:
			try {
				db.beginTransaction();
				rowDelete = db.delete(FoodTable.TABLE_NAME, selection,
						selectionArgs);
				db.setTransactionSuccessful();
			} catch (SQLException ex) {
				Log.e(TAG,
						"There was a problem with the delete in "
								+ FoodTable.TABLE_NAME + ", selection = "
								+ selection + ", selectionArgs = "
								+ selectionArgs.toString() + ex.toString());
			} finally {
				db.endTransaction();
			}
			break;
		case PLACE_KEY:
			try {
				db.beginTransaction();
				rowDelete = db.delete(PlaceTable.TABLE_NAME, selection,
						selectionArgs);
				db.setTransactionSuccessful();
			} catch (SQLException ex) {
				Log.e(TAG,
						"There was a problem with the delete in "
								+ PlaceTable.TABLE_NAME + ", selection = "
								+ selection + ", selectionArgs = "
								+ selectionArgs.toString() + ex.toString());
			} finally {
				db.endTransaction();
			}
			break;
		case RESERVATION_KEY:
			try {
				db.beginTransaction();
				rowDelete = db.delete(ReservationTable.TABLE_NAME, selection,
						selectionArgs);
				db.setTransactionSuccessful();
			} catch (SQLException ex) {
				Log.e(TAG,
						"There was a problem with the delete in "
								+ ReservationTable.TABLE_NAME + ", selection = "
								+ selection + ", selectionArgs = "
								+ selectionArgs.toString() + ex.toString());
			} finally {
				db.endTransaction();
			}
			break;
		default:
			throw new IllegalArgumentException("Unknown URI: " + uri);
		}
		return rowDelete;
	}

	@Override
	public String getType(Uri uri) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		// TODO Auto-generated method stub
		Uri result_uri = null;
		SQLiteDatabase db = null;
		db = mDatabaseHelper.getWritableDatabase();
		int uriKey = sUriMatcher.match(uri);
		switch (uriKey) {
		case PROMOTION_KEY:
			long id0 = db.insertWithOnConflict(PromotionTable.TABLE_NAME, null,
					values, SQLiteDatabase.CONFLICT_REPLACE);
			result_uri = Uri.parse(PROMOTION_CONTENT_URI + "/"
					+ Long.toString(id0));
			break;
		case DISH_CATEGORY_KEY:
			long id1 = db.insertWithOnConflict(DishCategoryTable.TABLE_NAME, null,
					values, SQLiteDatabase.CONFLICT_REPLACE);
			result_uri = Uri.parse(DISH_CATEGORY_CONTENT_URI + "/"
					+ Long.toString(id1));
			break;
		case FOOD_KEY:
			long id2 = db.insertWithOnConflict(FoodTable.TABLE_NAME, null,
					values, SQLiteDatabase.CONFLICT_REPLACE);
			result_uri = Uri.parse(FOOD_CONTENT_URI + "/"
					+ Long.toString(id2));
			break;
		case PLACE_KEY:
			long id3 = db.insertWithOnConflict(PlaceTable.TABLE_NAME, null,
					values, SQLiteDatabase.CONFLICT_REPLACE);
			result_uri = Uri.parse(PLACE_CONTENT_URI + "/"
					+ Long.toString(id3));
			break;
		case RESERVATION_KEY:
			long id4 = db.insertWithOnConflict(ReservationTable.TABLE_NAME, null,
					values, SQLiteDatabase.CONFLICT_REPLACE);
			result_uri = Uri.parse(RESERVATION_CONTENT_URI + "/"
					+ Long.toString(id4));
			break;
		default:
			throw new IllegalArgumentException("Unknown URI: " + uri);
		}
		db.close();
		return result_uri;
	}

	@Override
	public int bulkInsert(Uri uri, ContentValues[] values) {
		// TODO Auto-generated method stub
		int rowsAdded = 0;
		SQLiteDatabase db = null;
		db = mDatabaseHelper.getWritableDatabase();
		int uriKey = sUriMatcher.match(uri);
		switch (uriKey) {
		case PROMOTION_KEY:
			long rowId0;
			try {
				db.beginTransaction();

				for (ContentValues value : values) {
					rowId0 = db.insertOrThrow(PromotionTable.TABLE_NAME, null,
							value);
					if (rowId0 > 0)
						rowsAdded++;
				}
				db.setTransactionSuccessful();
			} catch (SQLException ex) {
				Log.e(TAG, "There was a problem with the bulk insert: "
						+ PromotionTable.TABLE_NAME + ". " + ex.toString());
			} finally {
				db.endTransaction();
			}
			break;
		case DISH_CATEGORY_KEY:
			long rowId1;
			try {
				db.beginTransaction();

				for (ContentValues value : values) {
					rowId1 = db.insertOrThrow(DishCategoryTable.TABLE_NAME, null,
							value);
					if (rowId1 > 0)
						rowsAdded++;
				}
				db.setTransactionSuccessful();
			} catch (SQLException ex) {
				Log.e(TAG, "There was a problem with the bulk insert: "
						+ DishCategoryTable.TABLE_NAME + ". " + ex.toString());
			} finally {
				db.endTransaction();
			}
			break;
		case FOOD_KEY:
			long rowId2;
			try {
				db.beginTransaction();

				for (ContentValues value : values) {
					rowId2 = db.insertOrThrow(FoodTable.TABLE_NAME, null,
							value);
					if (rowId2 > 0)
						rowsAdded++;
				}
				db.setTransactionSuccessful();
			} catch (SQLException ex) {
				Log.e(TAG, "There was a problem with the bulk insert: "
						+ FoodTable.TABLE_NAME + ". " + ex.toString());
			} finally {
				db.endTransaction();
			}
			break;
		case PLACE_KEY:
			long rowId3;
			try {
				db.beginTransaction();

				for (ContentValues value : values) {
					rowId3 = db.insertOrThrow(PlaceTable.TABLE_NAME, null,
							value);
					if (rowId3 > 0)
						rowsAdded++;
				}
				db.setTransactionSuccessful();
			} catch (SQLException ex) {
				Log.e(TAG, "There was a problem with the bulk insert: "
						+ PlaceTable.TABLE_NAME + ". " + ex.toString());
			} finally {
				db.endTransaction();
			}
			break;
		case RESERVATION_KEY:
			long rowId4;
			try {
				db.beginTransaction();

				for (ContentValues value : values) {
					rowId4 = db.insertOrThrow(ReservationTable.TABLE_NAME, null,
							value);
					if (rowId4 > 0)
						rowsAdded++;
				}
				db.setTransactionSuccessful();
			} catch (SQLException ex) {
				Log.e(TAG, "There was a problem with the bulk insert: "
						+ ReservationTable.TABLE_NAME + ". " + ex.toString());
			} finally {
				db.endTransaction();
			}
			break;
		default:
			throw new IllegalArgumentException("Unknown URI: " + uri);
		}

		return rowsAdded;
	}

	@Override
	public boolean onCreate() {
		// TODO Auto-generated method stub
		mDatabaseHelper = new DatabaseHelper(getContext());
		return true;
	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		// TODO Auto-generated method stub
		Cursor cursor = null;
		SQLiteDatabase db = null;
		db = mDatabaseHelper.getWritableDatabase();
		SQLiteQueryBuilder querybuilder = new SQLiteQueryBuilder();
		int uriKey = sUriMatcher.match(uri);
		switch (uriKey) {
		case PROMOTION_KEY:
			querybuilder.setTables(PromotionTable.TABLE_NAME);
			cursor = querybuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder);
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
			break;
		case DISH_CATEGORY_KEY:
			querybuilder.setTables(DishCategoryTable.TABLE_NAME);
			cursor = querybuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder);
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
			break;
		case FOOD_KEY:
			querybuilder.setTables(FoodTable.TABLE_NAME);
			cursor = querybuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder);
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
			break;
		case PLACE_KEY:
			querybuilder.setTables(PlaceTable.TABLE_NAME);
			cursor = querybuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder);
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
			break;
		case RESERVATION_KEY:
			querybuilder.setTables(ReservationTable.TABLE_NAME);
			cursor = querybuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder);
			cursor.setNotificationUri(getContext().getContentResolver(), uri);
			break;
		default:
			throw new IllegalArgumentException("Unknown URI: " + uri);
		}
		return cursor;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		// TODO Auto-generated method stub
		int row_update = 0;
		SQLiteDatabase db = null;
		db = mDatabaseHelper.getWritableDatabase();
		int uriKey = sUriMatcher.match(uri);
		switch (uriKey) {
		case PROMOTION_KEY:
			row_update = db.update(PromotionTable.TABLE_NAME, values, selection, selectionArgs);
			break;
		case DISH_CATEGORY_KEY:
			row_update = db.update(DishCategoryTable.TABLE_NAME, values, selection, selectionArgs);
			break;
		case FOOD_KEY:
			row_update = db.update(FoodTable.TABLE_NAME, values, selection, selectionArgs);
			break;
		case PLACE_KEY:
			row_update = db.update(PlaceTable.TABLE_NAME, values, selection, selectionArgs);
			break;
		case RESERVATION_KEY:
			row_update = db.update(ReservationTable.TABLE_NAME, values, selection, selectionArgs);
			break;
		default:
			throw new IllegalArgumentException("Unknown URI: " + uri);
		}
		return row_update;
	}

}
